package com.fancon.android.gardenjokesx.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

import com.fancon.android.gardenjokesx.GardenJokesBaseActivity;
import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.adapter.ListUserTitleAdapter;
import com.fancon.android.gardenjokesx.entity.ReportErrors;
import com.fancon.android.gardenjokesx.entity.User;
import com.fancon.android.gardenjokesx.rpc.GardenJokesApiConnect;
import com.fancon.android.gardenjokesx.rpc.JsonPaser;
import com.fancon.android.gardenjokesx.utils.Constant;
import com.fancon.android.gardenjokesx.utils.TimeUtil;
import com.fancon.android.gardenjokesx.utils.UserUtil;
import com.fancon.android.multithread.RequestUI;

public class M042Activity extends GardenJokesBaseActivity{
	private ArrayList<User> mListUser = new ArrayList<User>();
	private String mUserId;
	private String mViewedUserId;
	private int mPageIndex;
	private String mPhotoId;
	private String mIsFollowing;
//	PullToRefreshListView mPTRListUserFollow;
	ListUserTitleAdapter mListUserFollowAdapter;
	private ListView mListUserFollow;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.t03_layout);
		TextView tvTitle = (TextView) findViewById(R.id.t03_title);
		tvTitle.setText(getString(R.string.m042_title));
//		mPTRListUserFollow = (PullToRefreshListView) findViewById(R.id.t03_pull_refresh_list);
//		mListUserFollow = mPTRListUserFollow.getRefreshableView();
		mListUserFollow = (ListView) findViewById(R.id.t03_pull_refresh_list);
		mListUserFollow.setDivider(null);
		mUserId = UserUtil.getInfoUserLogin(this).getUser_id();
		//TODO Chưa get Viewed_userID
		Bundle extras = getIntent().getExtras(); 
		if (extras != null) {
			mIsFollowing = extras.getString("is_following");
			mViewedUserId = extras.getString(Constant.USER_ID_VIEW);
			mPhotoId = extras.getString(Constant.PHOTO_ID);
		}
		mPageIndex=1;
		GetListUserFollow request = new GetListUserFollow("m042", M042Activity.this);
		getGlobalState().getRequestQueue().addRequest(request);
		//@binhbt-comment
//		mPTRListUserFollow.setOnRefreshListener(new OnRefreshListener() {
//
//			@Override
//			public void onRefresh() {
//				// TODO Auto-generated method stub
//				mPageIndex=1;
//				mListUserFollow.setAdapter(null);
//				getListUserFollow request = new getListUserFollow("m042", getParent() != null? getParent():M042Activity.this);
//				getGlobalState().getRequestQueue().addRequest(request);
//			}
//		});
//		mPTRListUserFollow.setOnLoadmoreListener(new OnLoadmoreListener() {
//
//			@Override
//			public void onLoadmore() {
//				// TODO Auto-generated method stub
//				mPageIndex++;
//				getListUserFollow request = new getListUserFollow("m042", getParent() != null? getParent():M042Activity.this);
//				getGlobalState().getRequestQueue().addRequest(request);
//			}
//		});
	}
	public void regFollow(int position,String followingUserId,String isFollowing) {
		RegisterUserFollow request = new RegisterUserFollow(TimeUtil.getCurrentTime(),getParent() != null? getParent():M042Activity.this,position,mUserId,followingUserId,isFollowing);
		getGlobalState().getRequestQueue().addRequest(request);
	}
	
	class RegisterUserFollow extends RequestUI {
		  
		private Activity context;
		int position;
		String mUserId,mFollowingUserId,mIsFollowing;
		ReportErrors mReport;
		public RegisterUserFollow(Object key, Activity activity,int position,String userId, String followingUserId,String isFollowing) {
			super(key, activity);
			context = activity;
			this.position = position;
			mUserId = userId;
			mFollowingUserId = followingUserId;
			mIsFollowing = isFollowing;
			// TODO Auto-generated constructor stub
			
		}
		
		@Override
		public void execute() throws Exception {
			// TODO Auto-generated method stub
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
			nameValuePairs.add(new BasicNameValuePair("user_id",
					this.mUserId));
			nameValuePairs.add(new BasicNameValuePair("following_user_id",
					mFollowingUserId));
			nameValuePairs.add(new BasicNameValuePair("is_following",
					mIsFollowing));  
			String result="";
			try {
				GardenJokesApiConnect api = new GardenJokesApiConnect(context);
				result = api.execPost(getApplicationContext(), GardenJokesApiConnect.GROUP_S, "registerFollowingUser", nameValuePairs);
				Log.v("StringResult", result);
				
			} catch (Exception e) {
				// TODO: handle exception
				Log.d("Err", e.getMessage());
			}
			JsonPaser js = new JsonPaser();
			mReport = js.getReport(result);
		}

		@Override
		public void executeUI(Exception ex) {
			// TODO Auto-generated method stub
			//@binhbt-comment
			if(mReport.getResult()==0) mListUser.get(position).setIs_following(false);
			if(mReport.getResult()==1) mListUser.get(position).setIs_following(true);
			if(mReport.getResult()==2) mListUser.get(position).setIs_waitting(true);
			mListUserFollowAdapter.notifyDataSetChanged();
		}  
		  
	}
	public void confirmRequestFollow(int position, String requesterId, String isIgnore){
		ApprovedOrRejectContactRequest request = new ApprovedOrRejectContactRequest("requestFollow", getParent(),position,requesterId,isIgnore);
		getGlobalState().getRequestQueue().addRequest(request);
	}
	class ApprovedOrRejectContactRequest extends RequestUI {
		  
		private Activity context;
		ReportErrors mReport;
		int mPosition;
		String mRequesterId,mIsIgnore;
		public ApprovedOrRejectContactRequest(Object key, Activity activity,int position, String requesterId, String isIgnore) {
			super(key, activity);
			context = activity;
			mRequesterId = requesterId;
			mIsIgnore = isIgnore;
			mPosition =position;
			// TODO Auto-generated constructor stub
			
		}
		
  
		@Override
		public void execute() throws Exception {
			// TODO Auto-generated method stub
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
			nameValuePairs.add(new BasicNameValuePair("user_id",
					mUserId));
			nameValuePairs.add(new BasicNameValuePair("requester_id",
					mRequesterId));
			nameValuePairs.add(new BasicNameValuePair("is_ignored",
					mIsIgnore));
			String result="";
			try {
				GardenJokesApiConnect api = new GardenJokesApiConnect(context);
				result = api.execPost(getApplicationContext(), GardenJokesApiConnect.GROUP_U, "approvedOrRejectRequestContact", nameValuePairs);
				Log.v("StringResult",mUserId +"==="+ mRequesterId +"===="+mIsIgnore+ " === "+ result);
		
			} catch (Exception e) {
				// TODO: handle exception
				Log.d("Err", e.toString());     
			}
			JsonPaser js = new JsonPaser();
			mReport = js.getReport(result);
		}

		@Override
		public void executeUI(Exception ex) {
			// TODO Auto-generated method stub
			//@binhbt-comment
			//if(mReport.isIs_success()){
				//mListUser.remove(mPosition);
				//mListUserFollowAdapter.notifyDataSetChanged();
			//}
		    			
		}  
		  
	}
	class GetListUserFollow extends RequestUI {
		  
		private Activity context;
		String mAPI;
		//MeshProgressBar progress;
		public GetListUserFollow(Object key, Activity activity) {
			super(key, activity);
			context = activity;
			// TODO Auto-generated constructor stub
			//if(mListUser.size()==0){
			//	progress = MeshProgressBar.show((MeshGroupActivity)context.getParent()!=null?(MeshGroupActivity)context.getParent():context, context.getString(R.string.common_loading));
			//}
			//mPTRListUserFollow.removeNoHit();
		}
		
  
		@Override
		public void execute() throws Exception {
			// TODO Auto-generated method stub
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
			nameValuePairs.add(new BasicNameValuePair("user_id",
					mUserId));
			if(mViewedUserId==null){
				mAPI = "getListUserRatingPhoto";
				nameValuePairs.add(new BasicNameValuePair("photo_id",
						mPhotoId));
			}else {
				mAPI = "getListUserFollow";
				nameValuePairs.add(new BasicNameValuePair("viewed_user_id",
						mViewedUserId));
				nameValuePairs.add(new BasicNameValuePair("is_following",
						mIsFollowing));	
			}
			
			nameValuePairs.add(new BasicNameValuePair("page_index",
					mPageIndex+""));
			String result="";
			try {
				GardenJokesApiConnect api = new GardenJokesApiConnect(context);
				result = api.execGet(getApplicationContext(), GardenJokesApiConnect.GROUP_M, mAPI, nameValuePairs);
				Log.v("StringResult", result);
			} catch (Exception e) {
				// TODO: handle exception
				Log.d("Err", e.toString());  
			}
			if(mPageIndex==1){
				mListUser.clear();
				//TimeUtil.setLastRefresh(getApplicationContext(), Constant.M042);
			}
			JsonPaser js = new JsonPaser();
			mListUser.addAll(js.getListUser(result));
			Log.d("So phan tu",mListUser.size()+"");
		}

		@Override
		public void executeUI(Exception ex) {
			// TODO Auto-generated method stub
			//@binhbt-comment
//			if(progress!=null) progress.dismiss();
//		    mPTRListUserFollow.onRefreshComplete();
//		    mPTRListUserFollow.setTime(TimeUtil.getLastRefresh(getApplicationContext(), Constant.M042));
//			if (mListUser.size() == 0) {
//				mPTRListUserFollow.setNoHit();
//			} else {
//					mPTRListUserFollow.removeNoHit();
//					
//			}
//			mListUserFollowAdapter = new ListUserTitleAdapter(M042Activity.this, mListUser,Constant.M042);
//			mListUserFollow.setAdapter(mListUserFollowAdapter);
			
		}  
		  
	}
	
}
